<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: SettingHomeImage.php
// +----------------------------------------------------------------------
// | Author: ljf
// +----------------------------------------------------------------------
// | Description: 后台的轮播图信息
// +----------------------------------------------------------------------
// | Others:
// +----------------------------------------------------------------------
// | History:
// +----------------------------------------------------------------------
// | 1.Date: 2018/1/3 Author: ljf Modification: 创建
// +----------------------------------------------------------------------
namespace App\Http\Controllers\Backend\Setting;

use App\Constants\RedisKey;
use App\Events\SettingLogEvent;
use App\Http\Requests\Backend\Setting\SettingHomeImageRequest;
use App\Models\SettingHomeImage;
use App\Http\Controllers\Controller;
use App\Models\SettingLog;
class SettingHomeImageController extends Controller
{
    use \App\Library\Traits\SettingLog;
    const SETTING_TABLE = 'setting_home_images';
    public function __construct()
    {
        $this->middleware('auth:backend');
        $this->user = auth('backend')->user();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $setting_home = new SettingHomeImage();
        $setting_home->setConnection('backend');
        $imageInfo = $setting_home->select('id','img_name','img_url','url_set','is_show') -> orderBy('id','desc') -> get();
        foreach ($imageInfo as $k => &$v){
            $v['path'] = $v['img_url'];
            $v['img_url'] = getOssUrl($v['img_url']);
        }
        return success($imageInfo);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SettingHomeImageRequest $request)
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $setting_home = new SettingHomeImage();
        $setting_home->setConnection('backend');
        $imageInfo = $setting_home->where('id',$id) -> first();
        if($imageInfo){
            $imageInfo["img_url"] = getOssUrl($imageInfo['img_url']);
            return success($imageInfo);
        }
        return failed('当前信息不存在');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SettingHomeImageRequest $request, $id)
    {
        $updateData = $request->only(['img_url','img_name','is_show','url_set']);
        if(!empty($updateData['img_url'])){
            //删除原来的图片信息
            SettingHomeImage::deleteImage($id,$updateData['img_url']);
        }
        $info = SettingHomeImage::where('id',$id)->update($updateData);
        if($info){
            \Cache::delete(RedisKey::SETTING_HOME_IMAGE);
            $this->setting_log($this->user->id,self::SETTING_TABLE,$id,SettingLog::LOG_UPDATE,$updateData);
            return updated('');
        }
        return failed('更新失败');


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = SettingHomeImage::where('id',$id) -> update(['img_url' => '','img_name' => '','is_show' => 0,'url_set' => '']);
        if($result){
            \Cache::delete(RedisKey::SETTING_HOME_IMAGE);
            $this->setting_log($this->user->id,self::SETTING_TABLE,$id,SettingLog::LOG_DELETE);
            return deleted();
        }else{
            return failed('删除失败');
        }
    }
}
